var RSA = require('../utils/wx_rsa.js')
import
request from '../http/request.js'
import axios from 'axios'
import store from '@/store'
import md5 from 'js-md5';
// import { promises } from 'fs';
// import { resolve } from 'path';
export var userinfo;
export function setUser(key) {
    userinfo = key
}

function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}
var key =
    "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVq7n9eWgCZHqVYjAzTUqQ7IV1r0925TPxu13mBdRoI2J1Gdv+CC08DDIUtpmTTex2PUen5WgkJoUKY/h9aY/CCbBsiwtxaEUfUOeJuP/LAOugdi/taF337jniZEXM77Gtzq6XpMmw9BAvjJomoxzWh+4CjEyZtKtn6fvWisO7HwIDAQAB-----END PUBLIC KEY-----";
export function staticToken() {
    let uuid = guid();
    let psd = 'guest#' + uuid
    var encrypt_rsa = new RSA.RSAKey();
    encrypt_rsa = RSA.KEYUTIL.getKey(key);
    encrypt_rsa = encrypt_rsa.encrypt(psd)
    encrypt_rsa = RSA.hex2b64(encrypt_rsa);
    // console.log("解密："+encrypt_rsa.decrypt(encrypt_rsa))
    // return request({
    // 	url: "http://120.79.31.191:21000/guest/login",
    // 	method: 'post',
    // 	params: {
    // 		accountType: "guest",
    // 		account: uuid,
    // 		password: encrypt_rsa
    // 	}
    // })
    return new Promise((resolve, reject) => {
        axios.post("http://120.79.31.191:21000/guest/login", {
            accountType: "guest",
            account: uuid,
            password: encrypt_rsa
        }, {
            headers: {
                'Content-Type': 'application/json; charset=UTF-8',
                'YF-JWT': store.getters.token.JWT || '',
                'PF': "android",
                'YF-BID': "zwy",
                'YF-VER': '1.0.0',
                'YF-UID': store.getters.token.user.uid || ''
            }
        }).then(res => {
            const {
                data,
                status
            } = res
            if (status == 200 && data) {
                if (data.code == '0000') {
                    resolve(data)
                }
            }
        })
    })

}
export function superToken() {

    //    let user=[];
    //    if(userinfo){
    //     user=userinfo.split(":")
    //    }
    //    let account=user[0];
    //    let encrypt_rsa= decrypt(user[1]);

    let cacheKeys = {
        phone: '17779828887',
        pwd: 'zy123456'
    };
    let account = cacheKeys.phone
    let pwd = cacheKeys.pwd;
    var encrypt_rsa = new RSA.RSAKey();
    encrypt_rsa = RSA.KEYUTIL.getKey(key);
    encrypt_rsa = encrypt_rsa.encrypt(pwd)
    encrypt_rsa = RSA.hex2b64(encrypt_rsa);
    return new Promise((resolve, reject) => {
        axios.post("http://120.79.31.191:21000/user/login", {
            phone: account,
            //bid: "yw",
            pwd: encrypt_rsa,
            //accountType: cacheKeys1,
        }, {
            headers: {
                'Content-Type': 'application/json; charset=UTF-8',
                'YF-JWT': store.getters.token.JWT || '',
                'PF': "android",
                'YF-BID': "zwy",
                'YF-VER': '1.0.0',
                'YF-UID': store.getters.token.user.uid || ''
            }
        }).then(res => {
            const {
                data,
                status
            } = res
            if (status == 200 && data) {
                if (data.code == '0000') {
                    resolve(data)
                }
            }
        })
    })
}

export function getUrlKey(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let url = decodeURI(window.location.search);
    var r = url.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function decrypt(info) {
    let string = info.split("");
    // let index = JSON.parse(JSON.stringify(string[0]))
    let last = JSON.parse(JSON.stringify(string[string.length - 1]));
    string.unshift(last);
    string.pop()
    // string[string.length-1]=JSON.parse(JSON.stringify(index));
    return string = string.reverse().join("");
    console.log(string)
}